package com.seth.myapplication.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.seth.myapplication.bean.User;
import com.seth.myapplication.utils.DatabaseHelper.CrimeTable;

/**
 * Created by Seth on 2017/12/5.
 */

public class SqlDao {
    private DatabaseHelper mSqlte;
    private Context mContext;

    public SqlDao(Context context){
        mContext =context;

        //创建一个帮助类对象
        mSqlte = new DatabaseHelper(mContext);
    }

    /******************向数据库中添加用户***************/
    public boolean addUser(User user){
        //执行sql语句需要sqliteDatabase对象
        //调用getReadableDatabase方法,来初始化数据库的创建
        SQLiteDatabase database = mSqlte.getReadableDatabase();
        ContentValues values = new ContentValues();

        values.put(CrimeTable.Cols.ID,user.getUsrId());
        values.put(CrimeTable.Cols.NICKNAME,user.getNickname());
        values.put(CrimeTable.Cols.DETAILS,user.getDetail());
        values.put(CrimeTable.Cols.EMAILADDRESS,user.getEmail());
        values.put(CrimeTable.Cols.FROG,user.getFrog());
        values.put(CrimeTable.Cols.PHONE,user.getPhoneNum());
        values.put(CrimeTable.Cols.SEX, user.getSex());
        values.put(CrimeTable.Cols.STDNUM,user.getStdNum());
        values.put(CrimeTable.Cols.PASSWORD, user.getPassword());
        values.put(CrimeTable.Cols.REALNAME, user.getRealname());
        long result = database.insert("user", null, values);
        //底层是在拼装sql语句
        //关闭数据库对象
        database.close();
        if (result != -1) {//-1代表添加失败
            return true;
        } else {
            return false;
        }
    }
    /**************修改用户数据***********************/
    public Boolean updateUsr(User user) {

        //执行sql语句需要sqliteDatabase对象
        //调用getReadableDatabase方法,来初始化数据库的创建
        SQLiteDatabase db = mSqlte.getReadableDatabase();

        ContentValues values = new ContentValues();//是用map封装的对象，用来存放值
        //是用map封装的对象，用来存放值
        values.put(CrimeTable.Cols.NICKNAME,user.getNickname());
        values.put(CrimeTable.Cols.DETAILS,user.getDetail());
        values.put(CrimeTable.Cols.EMAILADDRESS,user.getEmail());
        values.put(CrimeTable.Cols.SEX, user.getSex());
        values.put(CrimeTable.Cols.STDNUM,user.getStdNum());
        values.put(CrimeTable.Cols.REALNAME, user.getRealname());
//        int result = db.update("music", values, "_ID = ?", new String[]{bean.getId() + ""});
        int result = db.update("user", values, CrimeTable.Cols.ID+"= ?", new String[]{user.getUsrId()+""});
        //关闭数据库对象
        db.close();
        if (result != -1) {//-1代表添加失败
            return true;
        } else {
            return false;
        }
    }

    /*****************删除用户名对应用户记录*************************/
    public int delUsr() {
        //执行sql语句需要sqliteDatabase对象
        //调用getReadableDatabase方法,来初始化数据库的创建
        SQLiteDatabase db = mSqlte.getReadableDatabase();
        int result = db.delete("user", null, null);
        //关闭数据库对象
        db.close();

        return result;
    }
    /***********************查询用户名对应密码**************************/
    public User usrQuery(){

        //执行sql语句需要sqliteDatabase对象
        //调用getReadableDatabase方法,来初始化数据库的创建
        SQLiteDatabase db = mSqlte.getReadableDatabase();

        Cursor cursor = db.query("user", new String[]{CrimeTable.Cols.NICKNAME,CrimeTable.Cols.DETAILS,CrimeTable.Cols.EMAILADDRESS,CrimeTable.Cols.
                FROG,CrimeTable.Cols.PHONE,CrimeTable.Cols.SEX,CrimeTable.Cols.STDNUM,CrimeTable.Cols.PASSWORD,CrimeTable.Cols.REALNAME, CrimeTable.Cols.ID}, null, null, null, null,null);
        User userInfo = new User();
        //List<String> password = new ArrayList<>();
        //解析Cursor中的数据
        if(cursor != null && cursor.getCount() >0){//判断cursor中是否存在数据
            cursor.moveToFirst();
            userInfo.setNickname(cursor.getString(0));
            userInfo.setDetail(cursor.getString(1));
            userInfo.setEmail(cursor.getString(2));
            userInfo.setFrog(cursor.getString(3));
            userInfo.setPhoneNum(cursor.getString(4));//这里尚未判空
            userInfo.setSex(cursor.getInt(5));
            userInfo.setStdNum(cursor.getString(6));
            userInfo.setPassword(cursor.getString(7));
            userInfo.setRealname(cursor.getString(8));
            userInfo.setUsrId(cursor.getInt(9));
            //password.add(cursor.getString(0));
            //password.add(cursor.getString(1));
            cursor.close();//关闭结果集
        }else{
            userInfo = null;
        }
        //关闭数据库对象
        db.close();
        return userInfo;
    }


}
